﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditSecurity : Form
    {
        private string sEditingSecurity = "";
        bool bEditing = false;

        public EditSecurity()
        {
            InitializeComponent();
        }

        public EditSecurity(string security)
        {
            sEditingSecurity = security;
            InitializeComponent();
            AcceptButton = buttonOK;
        }

        private void EditSecurity_Load(object sender, EventArgs e)
        {
            if (sEditingSecurity != "")
            {
                comboBoxSymbol.Items.Add(sEditingSecurity);
                comboBoxSymbol.SelectedIndex = 0;
                comboBoxSymbol.Enabled = false;
                bEditing = true;
                comboBoxSymbol_SelectionChangeCommitted(null, null);
                return;
            }

            try
            {
	            SecurityCollection securityCol = SecurityServices.GetSecurityCollection();
	            for (int iI = 0; iI < securityCol.Count; iI++)
	            {
	                comboBoxSymbol.Items.Add(securityCol[iI].Symbol);
	            }
            }
            catch (System.Exception ex)
            {
            	
            }

            EnableControls(false);
        }

        private void comboBoxSymbol_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Security security = null;
            try
            {
                security = SecurityServices.GetSecurityBySymbol(comboBoxSymbol.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid symbol   " + comboBoxSymbol.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            if (security == null)
            {
                MessageBox.Show("Invalid symbol   " + comboBoxSymbol.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            EnableControls(true);

            textBoxMarginPercentage.Text = security.MarginPercentage.ToString();
            textBoxMarketValue.Text = security.MarketValue.ToString();
            textBoxName.Text = security.Name;

            bEditing = true;
            sEditingSecurity = comboBoxSymbol.Text;
        }

        private void EnableControls(bool bEnable)
        {
            textBoxName.Enabled = bEnable;
            textBoxMarginPercentage.Enabled = bEnable;
            textBoxMarketValue.Enabled = bEnable;
            buttonOK.Enabled = bEnable;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Security security = new Security();
            security.Symbol = comboBoxSymbol.Text;
            security.Name = textBoxName.Text;
            security.MarketValue = double.Parse(textBoxMarketValue.Text);
            security.MarginPercentage = double.Parse(textBoxMarginPercentage.Text);

            try
            {
                SecurityServices.Update(security);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update security information\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Security information updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            double fMarginPercentage = 0;
            if (double.TryParse(textBoxMarginPercentage.Text, out fMarginPercentage) == false)
            {
                MessageBox.Show("Invalid margin percentage", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fMarketValue = 0;
            if (double.TryParse(textBoxMarketValue.Text, out fMarketValue) == false)
            {
                MessageBox.Show("Invalid market value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }
    }
}
